package net.zigin.nhi.could.waste.service.impl;

import net.zigin.commons.util.AssertUtil;
import net.zigin.commons.util.ParamsMapUtil;
import net.zigin.commons.util.ReflectUtil;
import net.zigin.commons.util.StringUtil;
import net.zigin.nhi.could.waste.aop.LoginAspect;
import net.zigin.nhi.could.waste.dao.*;
import net.zigin.nhi.could.waste.dto.NoticeMessageDto;
import net.zigin.nhi.could.waste.model.HospitalDepart;
import net.zigin.nhi.could.waste.model.NoticeReceive;
import net.zigin.nhi.could.waste.model.SysRole;
import net.zigin.nhi.could.waste.service.NoticeReceiveService;
import net.zigin.nhi.could.waste.service.SysUserService;
import net.zigin.nhi.could.waste.util.HtmlUtils;
import net.zigin.nhi.could.waste.vo.NoticeMessageVo;
import net.zigin.nhi.could.waste.vo.SysUserHospitalVo;
import net.zigin.nhi.could.waste.vo.SysUserVo;
import net.zigin.nhi.could.waste.vo.TokenInfoBo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.zigin.nhi.could.waste.model.NoticeMessage;
import net.zigin.nhi.could.waste.service.NoticeMessageService;

import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/*#################################################################*
# 说明:以下代码通过哲勤代码生成工具自动生成    
# 工具:GeneratorCode
# 作者:mgw1986                                                  
# 生成时间:2021-9-14 15:44:48                                               
##################################################################*/


/**
 * NoticeMessage服务实现类
 *
 * @author mgw1986
 * @version V1.0
 * @ClassName: NoticeMessageServiceImpl
 * @date 2021-9-14 15:44:48
 */
@Service
public class NoticeMessageServiceImpl extends AbstractServiceImpl<NoticeMessage, String> implements NoticeMessageService {
    @Autowired
    private NoticeMessageMapper noticeMessageMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private NoticeReceiveMapper noticeReceiveMapper;
    @Autowired
    private SysUserHospitalMapper sysUserHospitalMapper;
    @Autowired
    private SysRoleMapper sysRoleMapper;
    @Autowired
    private HospitalDepartMapper hospitalDepartMapper;

    @Override
    public BaseMapper<NoticeMessage, String> getMapper() {
        return noticeMessageMapper;
    }

    @Override
    public List<NoticeMessageVo> getList(NoticeMessageDto noticeMessageDto) {
        return null;
    }

    @Override
    public Integer getCount(NoticeMessageDto noticeMessageDto) {
        return null;
    }

    @Override
    public List<NoticeMessageVo> getCurrentUserList() {
        SysUserVo currentUser = sysUserService.getCurrentUser();
        String userId = currentUser.getId();
        List<NoticeMessageVo> list = noticeMessageMapper.getListByUserId(userId);
        list.forEach(noticeMessageVo -> {
            if(!StringUtil.isNullOrEmpty(noticeMessageVo.getPicUrl())){
                String[] split = noticeMessageVo.getPicUrl().split(",");
                noticeMessageVo.setPicUrls(Arrays.asList(split));
            }
            noticeMessageVo.setContent(HtmlUtils.delHtmlTag(noticeMessageVo.getContent()));
        });
        return list;
    }

    @Override
    public void read(String id) {
        SysUserVo currentUser = sysUserService.getCurrentUser();
        String userId = currentUser.getId();
        noticeReceiveMapper.read(userId, id);
    }

    @Override
    public NoticeMessageVo getInfoById(String id) {
        NoticeMessage noticeMessage = noticeMessageMapper.selectByPrimaryKey(id);
        NoticeMessageVo noticeMessageVo = ReflectUtil.copyProperties(noticeMessage, NoticeMessageVo.class);
        noticeMessageVo.setPicUrls(Arrays.asList(noticeMessageVo.getPicUrl().split(",")));
        if (!StringUtil.isNullOrEmpty(noticeMessage.getType())) {
            if (noticeMessage.getType().equals("role")) {
                SysRole sysRole = sysRoleMapper.selectByPrimaryKey(noticeMessageVo.getReceiverRoleId());
                noticeMessageVo.setReceiverRoleName(sysRole.getName());
            } else if (noticeMessage.getType().equals("depart")) {
                HospitalDepart hospitalDepart = hospitalDepartMapper.selectByPrimaryKey(noticeMessageVo.getReceiverDepartId());
                noticeMessageVo.setReceiverDepartName(hospitalDepart.getName());
            } else if (noticeMessage.getType().equals("staff")) {
                List<String> receiverIds = noticeReceiveMapper.getReceiverIds(noticeMessageVo.getId());
                noticeMessageVo.setReceiverIds(receiverIds);
            }
        }
        return noticeMessageVo;
    }

    @Override
    public String saveOrUpdate(NoticeMessageDto noticeMessageDto) {
        // 新建公告
        SysUserVo currentUser = sysUserService.getCurrentUser();
        noticeMessageDto.setHospitalDepartId(currentUser.getHospitalDepartId());
        noticeMessageDto.setHospitalBaseId(currentUser.getHospitalBaseId());
        StringBuffer picUrl = new StringBuffer();
        if (noticeMessageDto.getPicUrl() != null) {
            noticeMessageDto.getPicUrl().forEach(str -> {
                picUrl.append(str).append(",");
            });
        }
        // 新建 noticeReceive
        if (StringUtil.isNullOrEmpty(noticeMessageDto.getId())) {
            noticeMessageDto.setId(StringUtil.get32UUID());
            noticeMessageDto.setCreateUser(currentUser.getUserStaffName()); // 发布人 员工姓名
            noticeMessageDto.setCreateTime(new Date());
            noticeMessageDto.setModifyTime(new Date());
            NoticeMessage noticeMessage = ReflectUtil.copyProperties(noticeMessageDto, NoticeMessage.class);
            noticeMessage.setPicUrl(picUrl.toString());
            noticeMessageMapper.insert(noticeMessage);
            List<SysUserHospitalVo> revicers = getRevicers(noticeMessageDto);
            revicers.forEach(receiver -> {
                NoticeReceive noticeReceive = new NoticeReceive(receiver.getSysUserId(), noticeMessage.getId());
                noticeReceiveMapper.insert(noticeReceive);
            });
            return noticeMessage.getId();
        } else {
            noticeMessageDto.setModifyUser(currentUser.getName());
            noticeMessageDto.setModifyTime(new Date());
            NoticeMessage noticeMessage = ReflectUtil.copyProperties(noticeMessageDto, NoticeMessage.class);
            noticeMessage.setPicUrl(picUrl.toString());
            noticeMessageMapper.updateByPrimaryKeySelective(noticeMessage);
            return noticeMessage.getId();
        }
    }

    private List<SysUserHospitalVo> getRevicers(NoticeMessageDto noticeMessageDto) {
        // 通过 角色 科室 ids 医院id 获取 用户
        return sysUserHospitalMapper.getRevicersByCondition(noticeMessageDto);
    }
}